home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / cxe103.zip / CXF.PAS < prev    next >
Pascal/Delphi Source File  |  1992-08-03  |  6KB  |  227 lines

  1. program  Cxf; uses cx;
  2.  
  3. {
  4.    Copyright (c) 1990-1992 Eugene Nelson
  5.  
  6.    This program demonstrates the suggested usage of the Cx Data Compression
  7.    Library.  It will compress and decompress a file of any size, containing
  8.    any type of data.  The compressed data is stored with 16 bit CRC's to
  9.    help in detecting errors.  It stores the file as blocks of data which
  10.    have the following form:              
  11.  
  12.       BLOCK
  13.       ----------------------------------------------------
  14.       2 bytes - size of uncompressed block (USIZE)
  15.       2 bytes - size of compressed block   (CSIZE)
  16.       2 bytes - 16 bit CRC                 (CRC)
  17.       CSIZE   - data                       (DATA)
  18.       ----------------------------------------------------
  19.  
  20.    If a block cannot be compressed, the original data is stored, and
  21.    USIZE and CSIZE will be the same.  The CRC is computed on the
  22.    compressed data.
  23.  
  24.    2 bytes of 0 are stored at the end of the compressed file to indicate
  25.    the end of file.
  26. }
  27.  
  28.  
  29. {-------------------------------------------------------------------------}
  30. procedure QUIT(s: string);
  31. begin
  32.    writeln('');
  33.    writeln(s);
  34.    Halt;
  35. end;
  36.  
  37. {-------------------------------------------------------------------------}
  38. procedure usage;
  39. begin
  40.    writeln('usage: cxf 1|2|3|t|d infile outfile');
  41.    writeln('   1|2|3   - compress infile to outfile using method 1,2 or 3');
  42.    writeln('   d       - decompress infile to outfile');
  43.    writeln('   t       - test integrity of infile');
  44.    writeln('');
  45.    writeln('   Examples:');
  46.    writeln('      cxf 1 cxf.exe x.x');
  47.    writeln('      cxf t x.x');
  48.    writeln('      cxf d x.x y.y');
  49.    Halt;
  50. end;
  51.  
  52. {-------------------------------------------------------------------------}
  53. procedure file_compress(dst, src: string; method, size: CXINT);
  54. var
  55.    ifile, ofile: file;
  56.    ifilesize, ofilesize: longint;
  57.    ibuff, obuff, tbuff: pointer;
  58.    j, k, crc: CXINT;
  59.  
  60. begin
  61.    Assign(ifile, src);
  62.    Reset(ifile, 1);
  63.    ifilesize:= FileSize(ifile);
  64.  
  65.    Assign(ofile, dst);
  66.    Rewrite(ofile, 1);
  67.    ofilesize:= 0;
  68.  
  69.    GetMem(ibuff, size);
  70.    GetMem(obuff, size+CX_SLOP);
  71.    GetMem(tbuff, CX_C_MAXTEMP);
  72.  
  73.    repeat
  74.       write('.');
  75.  
  76.       BlockRead(ifile, ibuff^, size, j);
  77.       BlockWrite(ofile, j, CXINTSIZE);
  78.  
  79.       if j <> 0
  80.       then begin
  81.          k:= CX_COMPRESS(method, obuff^, size, ibuff^, j, tbuff^, CX_C_MAXTEMP);
  82.          case k of
  83.             CX_ERR_METHOD:
  84.                QUIT('unknown or unsupported method, evaluation version only allows CX_MEHTOD1');
  85.  
  86.             CX_ERR_BUFFSIZE:
  87.                QUIT('invallid input or output buffer size');
  88.  
  89.             CX_ERR_TEMPSIZE:
  90.                QUIT('invalid temporary buffer size');
  91.          end;
  92.  
  93.          BlockWrite(ofile, k, CXINTSIZE);
  94.  
  95.          if (k = j)     {if block could not be compressed}
  96.          then begin
  97.             crc:= CX_CRC(ibuff^, k);
  98.             BlockWrite(ofile, crc, CXINTSIZE);
  99.             BlockWrite(ofile, ibuff^, j)
  100.          end
  101.          else begin
  102.             crc:= CX_CRC(obuff^, k);
  103.             BlockWrite(ofile, crc, CXINTSIZE);
  104.             BlockWrite(ofile, obuff^, k);
  105.          end;
  106.  
  107.          ofilesize:= ofilesize + CXINTSIZE + CXINTSIZE + k;
  108.       end;
  109.    until j = 0;
  110.  
  111.    FreeMem(ibuff, size);
  112.    FreeMem(obuff, size+CX_SLOP);
  113.    FreeMem(tbuff, CX_C_MAXTEMP);
  114.  
  115.    ifilesize:= FileSize(ifile);
  116.    ofilesize:= FileSize(ofile);
  117.    Close(ifile);
  118.    Close(ofile);
  119.  
  120.    writeln('');
  121.    write('In Size:    ');  writeln(ifilesize);
  122.    write('Out Size:   ');  writeln(ofilesize);
  123.    if ifilesize <> 0
  124.    then begin
  125.       write('Percent:    '); writeln((ofilesize*100) div ifilesize);
  126.    end
  127. end;
  128.  
  129. {-------------------------------------------------------------------------}
  130. procedure file_decompress(dst, src: string);
  131. var
  132.    ifile, ofile: file;
  133.    ibuff, obuff, tbuff: pointer;
  134.    j, k, crc: CXINT;
  135.  
  136. begin
  137.    Assign(ifile, src);
  138.    Reset(ifile, 1);
  139.  
  140.    if dst <> ''
  141.    then begin
  142.       Assign(ofile, dst);
  143.       Rewrite(ofile, 1);
  144.    end;
  145.  
  146.    GetMem(ibuff, CX_MAX_BUFFER+CX_SLOP);
  147.    GetMem(obuff, CX_MAX_BUFFER);
  148.    GetMem(tbuff, CX_D_MINTEMP);
  149.  
  150.    repeat
  151.       write('.');
  152.  
  153.       BlockRead(ifile, j, CXINTSIZE);
  154.  
  155.       if j <> 0
  156.       then begin
  157.          BlockRead(ifile, k, CXINTSIZE);
  158.          BlockREad(ifile, crc, CXINTSIZE);
  159.          BlockRead(ifile, ibuff^, k);
  160.  
  161.          if CX_CRC(ibuff^, k) <> crc
  162.             then QUIT ('CRC error');
  163.  
  164.          if j = k
  165.          then begin
  166.             if dst <> ''
  167.                then BlockWrite(ofile, ibuff^, k);
  168.          end
  169.          else begin
  170.             k:= CX_DECOMPRESS(obuff^, CX_MAX_BUFFER, ibuff^, k, tbuff^, CX_D_MINTEMP);
  171.             case k of
  172.                CX_ERR_INVALID:
  173.                   QUIT('data being compressed is corrupt, or was not compressed with Cx.');
  174.  
  175.                CX_ERR_METHOD:
  176.                   QUIT('unknown or unsupported method, evaluation version only allows CX_MEHTOD1');
  177.  
  178.                CX_ERR_BUFFSIZE:
  179.                   QUIT('End of Data symbol not found');
  180.    
  181.                CX_ERR_TEMPSIZE:
  182.                   QUIT('invalid temporary buffer size');
  183.             end;
  184.  
  185.             if k <> j
  186.                then QUIT('data being compressed is corrupt, or was not compressed with Cx.');
  187.  
  188.             if dst <> ''
  189.                then BlockWrite(ofile, obuff^, j);
  190.          end;
  191.       end;
  192.    until j = 0;
  193.  
  194.    FreeMem(ibuff, CX_MAX_BUFFER+CX_SLOP);
  195.    FreeMem(obuff, CX_MAX_BUFFER);
  196.    FreeMem(tbuff, CX_D_MINTEMP);
  197.  
  198.    Close(ifile);
  199.    if dst <> ''
  200.       then Close(ofile);
  201. end;
  202.  
  203.  
  204. begin
  205.    if ParamCount < 3
  206.    then begin
  207.       if (ParamStr(1) = 't') or (ParamStr(1) = 'T')
  208.          then file_decompress('', ParamStr(2))
  209.          else usage
  210.    end
  211.    else begin
  212.       if ParamStr(1) = '1'
  213.          then file_compress(ParamStr(3), ParamStr(2), CX_METHOD1, CX_MAX_BUFFER);
  214.  
  215.       if ParamStr(1) = '2'
  216.          then file_compress(ParamStr(3), ParamStr(2), CX_METHOD2, CX_MAX_BUFFER);
  217.  
  218.       if ParamStr(1) = '3'
  219.          then file_compress(ParamStr(3), ParamStr(2), CX_METHOD3, CX_MAX_BUFFER);
  220.  
  221.       if (ParamStr(1) = 'd') or (ParamStr(1) = 'D')
  222.          then file_decompress(ParamStr(3), ParamStr(2));
  223.    end;
  224.  
  225.    QUIT ('Ok');
  226. end.
  227.